﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;
using CrystalDecisions.Shared;
using System.Drawing.Printing;
using LHS.Win.Report;
using LHS.Core.TableInfo;
using MS.Core.Base.Transaction;
using System.Collections.Specialized;
using MS.Core.Constants.Objects;
using System.IO;

namespace LHS.Win
{
    public partial class frmXemToaThuoc : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        Hashtable _hashParam = new Hashtable();
        byte[] _byteEmpSignature = null;
        // Methods
        public frmXemToaThuoc(frmHoSoBenhAn fHoSoBenhAn, Hashtable hashParam, byte[] byteEmpSignature)
        {
            _hashParam = hashParam; //edit here 20090531
            _byteEmpSignature = byteEmpSignature;// edit here 20090604

            this._fHoSoBenhAn = fHoSoBenhAn;
            this.InitializeComponent();
            this.dtView = new DataTable("ExportMedicineList");
            this.dtView.Columns.Add(InfoMedicineExport.MEXORDER);
            this.dtView.Columns.Add(InfoMedicineExport.MEXMEDICINENAME); // "Tên Thuốc"
            this.dtView.Columns.Add(InfoMedicineExport.MEXQUANTITYPERUNIT); //Số lượng
            this.dtView.Columns.Add(InfoMedicineExport.MEXPRODUCTIONPERUNIT);//"Đơn vị "
            this.dtView.Columns.Add(InfoMedicineExport.MEXTAKEPERDAY); // "Ngày SD/ Lần"
            this.dtView.Columns.Add(InfoMedicineExport.MEXQUANTITYPERDAY); //"Lần"
            this.dtView.Columns.Add(InfoMedicineExport.MEXUSAGEUNIT);//"Đơn Vị Dùng"
            this.dtView.Columns.Add(InfoMedicineExport.MEXUSAGEPERDAY);//"Cử "
            this.dtView.Columns.Add(InfoMedicineExport.MEXUSAGE); //"Cách SD "
            this.dtView.Columns.Add(InfoMedicineExport.MEXNOTE); // "Lưu ý "
            this.dtView.Columns.Add("YesOrNo");
            this.dtView.Columns.Add(InfoMedicineExport.MEXISN);

            //phan them moi
            this.dtView.Columns.Add(InfoMedicineExport.MEXTOTALQUANTITY);
            this.dtView.Columns.Add(InfoMedicineExport.MEXTOTALUSAGEDAY);
            this.dtView.Columns.Add(InfoMedicine.MIMMEDICINEORIGINALNAME); //ten goc -- edit 20090531
            //phan them moi 20090602
            this.dtView.Columns.Add(InfoMedicine.MEXMOREDETAILS); //more details
            //end phan them moi
            this.dsView = new DataSet();
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            if (this.check())
            {
                this.tbLanUong_Leave(sender, e);
                if (Convert.ToInt64(this.tbSoLuong.Text) == 0L)
                {
                    MessageBox.Show("Vui lòng nhập số lượng lớn hơn 0", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    DataRow row = this.dtView.NewRow();
                    ArrayList list = new ArrayList();
                    this.count = this.dtView.Rows.Count + 1;
                    list.Add(this.count.ToString());
                    if (this.txtNewMedicine.Text != "")
                    {
                        list.Add(this.txtNewMedicine.Text);
                    }
                    if (this.txtNewMedicine.Text == "")
                    {
                        list.Add(this.cboSick.SelectedItem.ToString());
                    }
                    list.Add(this.tbSoLuong.Text);
                    if (this.txtNewMedicine.Text != "")
                    {
                        list.Add(this.cboDonVi.SelectedItem.ToString());
                    }
                    if (this.txtNewMedicine.Text == "")
                    {
                        list.Add(this.txtDonViChuan.Text);
                    }
                    list.Add(this.tbNgayUong.Text);
                    list.Add(this.tbLanUong.Text);
                    list.Add(this.txtDonVi.Text);
                    list.Add(this.cboCuUong.Text);
                    list.Add(this.cboCachSD.Text); //thaoht edit 20090726
                    list.Add(this.cboLuuY.Text);
                    if (this.txtNewMedicine.Text != "")
                    {
                        list.Add("0");
                        list.Add(0);
                    }
                    if (this.txtNewMedicine.Text == "")
                    {
                        list.Add("1");
                        list.Add(this.txtISN.Text);
                    }

                    row[0] = list[0];
                    row[1] = list[1];
                    row[2] = list[2];
                    row[3] = list[3];
                    row[4] = list[4];
                    row[5] = list[5];
                    row[6] = list[6];
                    row[7] = list[7];
                    row[8] = list[8];
                    row[9] = list[9];
                    row[10] = list[10];
                    row[11] = list[11];

                    //phan them moi
                    list.Add(txtSumNgayDung.Value);
                    list.Add(txtSumSoLuong.Value);
                    list.Add(txtOriginalName.Text); //edit here 20090531
                    //phan them moi 20090602
                    list.Add(this.txtMoreDetails.text);

                    row[InfoMedicineExport.MEXTOTALUSAGEDAY] = list[12];
                    row[InfoMedicineExport.MEXTOTALQUANTITY] = list[13];
                    row[InfoMedicine.MIMMEDICINEORIGINALNAME] = list[14]; // edit here 20090531
                    row[InfoMedicine.MEXMOREDETAILS] = list[15]; // edit here 20090602
                    //end phan them moi

                    this.dtView.Rows.Add(row);
                    this.loadDataSet();
                    int num2 = 0;
                    for (int i = 0; i < this.arrTong.Count; i++)
                    {
                        if (!(this.arrTong[i].ToString() == this.txtISN.Text) || !(this.arrTong[i].ToString() != "0"))
                        {
                            continue;
                        }
                        MessageBox.Show("Vui lòng chọn tên thuốc khác. \n tên thuốc này đã tồn tại trong danh sách thuốc đã kê", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        for (int k = 0; k < this.dtView.Rows.Count; k++)
                        {
                            if (this.dsView.Tables["ExportMedicineList"].Rows[k][InfoMedicineExport.MEXISN].ToString() == this.txtISN.Text)
                            {
                                this.dsView.Tables["ExportMedicineList"].Rows[k].Delete();
                                break;
                            }
                        }
                        int num5 = 0;
                        while (num5 < this.dsView.Tables["ExportMedicineList"].Rows.Count)
                        {
                            this.dtView.Rows[num5][InfoMedicineExport.MEXORDER] = (num5 + 1).ToString();
                            return;
                        }
                    }
                    for (int j = 0; j < this.dsView.Tables["ExportMedicineList"].Rows.Count; j++)
                    {
                        if ((this.dsView.Tables["ExportMedicineList"].Rows[j][InfoMedicineExport.MEXISN].ToString() == this.txtISN.Text) && (this.dsView.Tables["ExportMedicineList"].Rows[j][InfoMedicineExport.MEXISN].ToString() != "0"))
                        {
                            num2++;
                        }
                        if (num2 == 2)
                        {
                            MessageBox.Show("Vui lòng chọn tên thuốc khác. \n tên thuốc này đã tồn tại trong danh sách thuốc đã kê", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            this.dsView.Tables["ExportMedicineList"].Rows[j].Delete();
                            for (int m = 0; m < this.dtView.Rows.Count; m++)
                            {
                                this.dtView.Rows[m][InfoMedicineExport.MEXORDER] = (m + 1).ToString();
                            }
                            return;
                        }
                    }
                    if (this.txtNewMedicine.Text != "")
                    {
                        this.cboDonVi.Visible = true;
                        this.txtDonViChuan.Visible = false;
                        this.cboDonVi.SelectedIndex = 0;
                    }
                    if (this.txtNewMedicine.Text == "")
                    {
                        this.cboDonVi.Visible = false;
                        this.txtDonViChuan.Visible = true;
                        this.txtDonViChuan.Text = "";
                    }
                    this.txtNewMedicine.Text = "";
                    this.txtDonViChuan.Text = "";
                    this.cboCachSD.Text = ""; //thaoht edit 20090726
                    this.tbSoLuong.Text = "";
                    this.txtDonVi.Text = "";
                    this.tbLanUong.Text = "";
                    this.tbNgayUong.Text = "";
                    this.cboSick.SelectedIndex = 0;
                    this.cboLuuY.Text = "";

                    //phan them moi
                    this.txtSumSoLuong.Value = 0;
                    this.txtSumNgayDung.Value = 0;
                    //phan them moi 20090602
                    this.txtOriginalName.Text = "";
                    this.txtMoreDetails.text = "";
                    //end phan them moi
                }
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if ((this.dataGrid1.DataSource != null) && (this.dsView.Tables["ExportMedicineList"].Rows.Count != 0))
            {
                if (this.tbSoLuong.Text == "")
                {
                    MessageBox.Show("Vui lòng chọn tên thuốc từ danh sách nhập", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    this.dsView.Tables["ExportMedicineList"].Rows[this.dataGrid1.CurrentRowIndex].Delete();
                    for (int i = 0; i < this.dsView.Tables["ExportMedicineList"].Rows.Count; i++)
                    {
                        if (this.dsView.Tables["ExportMedicineList"].Rows.Count == 0)
                        {
                            this.dataGrid1.DataSource = null;
                            return;
                        }
                        this.dsView.Tables["ExportMedicineList"].Rows[i][InfoMedicineExport.MEXORDER] = i + 1;
                    }
                    if (this.txtNewMedicine.Text == "")
                    {
                        this.arrTong.Remove(this.txtISN.Text);
                    }
                    this.txtNewMedicine.Text = "";
                    this.txtDonVi.Text = "";
                    this.cboCachSD.Text = "";//thaoht edit 20090726
                    this.tbSoLuong.Text = "";
                    this.txtDonVi.Text = "";
                    this.tbLanUong.Text = "";
                    this.tbNgayUong.Text = "";
                    this.cboSick.SelectedIndex = 0;
                    this.txtDonViChuan.Text = "";
                    this.cboDonVi.SelectedIndex = 0;
                    this.cboLuuY.Text = "";
                    if (this.txtNewMedicine.Text != "")
                    {
                        this.cboDonVi.Visible = true;
                        this.txtDonViChuan.Visible = false;
                    }
                    if (this.txtNewMedicine.Text == "")
                    {
                        this.cboDonVi.Visible = false;
                        this.txtDonViChuan.Visible = true;
                    }

                    //phan them moi
                    this.txtSumSoLuong.Value = 0;
                    this.txtSumNgayDung.Value = 0;
                    //phan them moi 20090602
                    this.txtOriginalName.Text = "";
                    this.txtMoreDetails.text = "";
                    //end phan them moi
                }
            }
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            if ((this.dataGrid1.DataSource != null) && (this.dsView.Tables["ExportMedicineList"].Rows.Count != 0))
            {
                TransactionManager trans = TransactionManager.ThreadTransactionMgr();
                try
                {
                    trans.BeginTransaction();

                    ListDictionary paramDelMedicineExport = new ListDictionary();
                    paramDelMedicineExport.Add(InfoClinicalRecord.CLRISN, this._fHoSoBenhAn.clrImageISN);
                    objController.Exec_StoreProc("sp_MedicineExport_Delete", paramDelMedicineExport);

                    string str = string.Empty;
                    ListDictionary param = new ListDictionary();
                    DataRow dr;
                    for (int i = 0; i < this.dsView.Tables["ExportMedicineList"].Rows.Count; i++)
                    {
                        dr = this.dsView.Tables["ExportMedicineList"].Rows[i];
                        switch (dr[InfoMedicineExport.MEXPRODUCTIONPERUNIT].ToString())
                        {
                            case "viên":
                                str = "0";
                                break;
                            case "ống":
                                str = "1";
                                break;
                            case "chai":
                                str = "2";
                                break;
                            case "gói":
                                str = "3";
                                break;
                            case "tuýp":
                                str = "4";
                                break;
                        }

                        param.Clear();
                        param.Add(InfoMedicineExport.MEXMEDICINENAME, dr[1].ToString());//1
                        param.Add(InfoMedicineExport.CLRISN, this.clrISN);//2
                        param.Add(InfoMedicineImport.MIMID, dr[InfoMedicineExport.MEXISN].ToString());//3
                        param.Add(InfoMedicineExport.MEXQUANTITYPERUNIT, dr[2].ToString());//4
                        param.Add(InfoMedicineExport.MEXTAKEPERDAY, dr[4].ToString());//5
                        param.Add(InfoMedicineExport.MEXPRODUCTIONPERUNIT, str);//6
                        param.Add(InfoMedicineExport.MEXQUANTITYPERDAY, dr[5].ToString());//7
                        param.Add(InfoMedicineExport.MEXUSAGEPERDAY, dr[7].ToString());//8
                        param.Add(InfoMedicineExport.MEXNOTE, dr[9].ToString());//9
                        param.Add(InfoMedicineExport.MEXUSAGE, dr[8].ToString());//10
                        param.Add(InfoMedicineExport.MEXUSAGEUNIT, dr[6].ToString());//11
                        param.Add(InfoInvoices.INVMEDICINEPAYMENT, 0);//12
                        //phan moi them
                        param.Add(InfoMedicineExport.MEXTOTALQUANTITY, dr[InfoMedicineExport.MEXTOTALQUANTITY].ToString());//13
                        param.Add(InfoMedicineExport.MEXTOTALUSAGEDAY, dr[InfoMedicineExport.MEXTOTALUSAGEDAY].ToString());//14
                        param.Add(InfoMedicineExport.MEXORDER, dr[InfoMedicineExport.MEXORDER].ToString());//14 --thaoht edit 20090726
                        //end phan moi them
                        objController.Exec_StoreProc("sp_MedicineExport_Insert", param);
                    }

                    this.btPrint.Enabled = true;
                    trans.CommitTransaction();
                    MessageBox.Show("Toa thuốc đã được cập nhật", "Cập Nhật Toa Thuốc", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                catch (Exception ex)
                {
                    trans.RollbackTransaction();
                    Class.ShowException(ex);
                }
            }
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            DataTable dt = dtView.Clone();
            //remove column MoreDetails vi no la ntext, khoi luong thong tin rat lon
            dt.Columns.Remove(InfoMedicine.MEXMOREDETAILS);//20090602
            dt.Columns.Add(InfoEmployees.EMPID, typeof(string));
            dt.Columns[InfoEmployees.EMPID].DefaultValue = _hashParam[InfoEmployees.EMPID].ToString();
            dt.Columns.Add(InfoEmployees.EMPNAME, typeof(string));
            dt.Columns[InfoEmployees.EMPNAME].DefaultValue = _hashParam[InfoEmployees.EMPNAME].ToString();
            dt.Columns.Add(InfoPatients.PATID, typeof(string));
            dt.Columns[InfoPatients.PATID].DefaultValue = _hashParam[InfoPatients.PATID].ToString();
            dt.Columns.Add(InfoPatients.PATNAME, typeof(string));
            dt.Columns[InfoPatients.PATNAME].DefaultValue = _hashParam[InfoPatients.PATNAME].ToString();
            dt.Columns.Add(InfoPatients.PATSEX, typeof(string));
            dt.Columns[InfoPatients.PATSEX].DefaultValue = _hashParam[InfoPatients.PATSEX].ToString();
            dt.Columns.Add(InfoPatients.PATAGE, typeof(string));
            dt.Columns[InfoPatients.PATAGE].DefaultValue = _hashParam[InfoPatients.PATAGE].ToString();
            dt.Columns.Add(InfoPatients.PATADDRESS, typeof(string));
            dt.Columns[InfoPatients.PATADDRESS].DefaultValue = _hashParam[InfoPatients.PATADDRESS].ToString();
            dt.Columns.Add(InfoClinicalRecord.CLRMEDICALADVICE, typeof(string));
            dt.Columns[InfoClinicalRecord.CLRMEDICALADVICE].DefaultValue = _hashParam[InfoClinicalRecord.CLRMEDICALADVICE].ToString();
            dt.Columns.Add(InfoClinicalRecord.CLRSKILL, typeof(string));
            dt.Columns[InfoClinicalRecord.CLRSKILL].DefaultValue = _hashParam[InfoClinicalRecord.CLRSKILL].ToString();
            dt.Columns.Add(InfoClinicalRecord.CLRPATHOLOGICALSIGNS, typeof(string));
            dt.Columns[InfoClinicalRecord.CLRPATHOLOGICALSIGNS].DefaultValue = _hashParam[InfoClinicalRecord.CLRPATHOLOGICALSIGNS].ToString();
            dt.Columns.Add(InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE, typeof(string));
            dt.Columns[InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE].DefaultValue = _hashParam[InfoClinicalRecord.CLRFOLLOWUPEXAMINATIONDATE].ToString();

            foreach (DataRow dr in dtView.Select("", InfoMedicineExport.MEXORDER + " asc"))
            {
                dt.ImportRow(dr);
            }

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            rptToaThuoc thuoc = new rptToaThuoc();

            /// edit by thaoht 20090604
            /// phan chữ ký điện tử
            if (_byteEmpSignature != null)
            {
                MemoryStream stream = new MemoryStream(_byteEmpSignature);
                thuoc.picSigniture.Image = Image.FromStream(stream);
                stream.Close();
                stream.Flush();
            }
            ///end phan edit chữ ký

            thuoc.lbNgayInToa.Text = (dtpDate.Value.Day < 10 ? "0" + dtpDate.Value.Day.ToString() : dtpDate.Value.Day.ToString()) + " tháng " + (dtpDate.Value.Month < 10 ? "0" + dtpDate.Value.Month.ToString() : dtpDate.Value.Month.ToString()) + " năm " + dtpDate.Value.Year.ToString(); //thiet lap ngay in toa --20090602
            thuoc.DataSource = ds;

            /* cach 2
             * this.printDialog1.Document = this.printDocument1;
             DialogResult result = this.printDialog1.ShowDialog();
             //PrintDocument document = new PrintDocument();
             if (result == DialogResult.OK)
             {
                 this.tenmayin = document.PrinterSettings.PrinterName;// document.PrinterSettings.PrinterName;
                 thuoc.PrinterName  = tenmayin;
                 thuoc.PaperKind = PaperKind.A5;
                 thuoc.Print();
             }
             */

            //cach 1
            this.tenmayin = this.printDocument1.PrinterSettings.PrinterName;
            thuoc.PrinterName = tenmayin;
            thuoc.Print();
        }

        private void btThoát_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            if ((this.dataGrid1.DataSource != null) && (this.dsView.Tables["ExportMedicineList"].Rows.Count != 0))
            {
                if (this.tbSoLuong.Text == "")
                {
                    MessageBox.Show("Vui lòng chọn tên thuốc từ danh sách nhập", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else if (this.check())
                {
                    int num = 0;
                    for (int i = 0; i < this.dsView.Tables["ExportMedicineList"].Rows.Count; i++)
                    {
                        if (this.dsView.Tables["ExportMedicineList"].Rows[i][InfoMedicineExport.MEXISN].ToString() == this.txtISN.Text)
                        {
                            num++;
                        }
                        if (num == 2)
                        {
                            MessageBox.Show("Vui lòng chọn tên thuốc khác. \n tên thuốc này đã tồn tại trong danh sách thuốc đã kê", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            this.dsView.Tables["ExportMedicineList"].Rows[i].Delete();
                            return;
                        }
                    }

                    DataRow dr;
                    for (int j = 0; j < this.dsView.Tables["ExportMedicineList"].Rows.Count; j++)
                    {
                        dr = this.dsView.Tables["ExportMedicineList"].Rows[j];
                        if ((dr[InfoMedicineExport.MEXORDER].ToString() == this.txtSTT.Text) && (dr[InfoMedicineExport.MEXISN].ToString() != "0"))
                        {
                            if (this.txtNewMedicine.Text != "")
                            {
                                this.cboDonVi.Visible = true;
                                this.txtDonViChuan.Visible = false;
                                dr[1] = this.txtNewMedicine.Text;
                                dr[3] = this.cboDonVi.SelectedItem.ToString();
                                this.cboDonVi.SelectedIndex = 0;
                                dr[InfoMedicineExport.MEXISN] = "0";
                            }
                            else
                            {
                                this.cboDonVi.Visible = false;
                                this.txtDonViChuan.Visible = true;
                                dr[1] = this.cboSick.SelectedItem.ToString();
                                dr[3] = this.txtDonViChuan.Text;
                                dr[InfoMedicineExport.MEXISN] = this.txtISN.Text;
                                this.txtDonViChuan.Text = "";
                            }
                            dr[2] = this.tbSoLuong.Text;
                            dr[4] = this.tbNgayUong.Text;
                            dr[5] = this.tbLanUong.Text;
                            dr[6] = this.txtDonVi.Text;
                            dr[7] = this.cboCuUong.Text;
                            dr[8] = this.cboCachSD.Text;//thaoht edit 20090726
                            dr[9] = this.cboLuuY.Text;

                            //phan them moi
                            dr[InfoMedicineExport.MEXTOTALUSAGEDAY] = this.txtSumNgayDung.Value;
                            dr[InfoMedicineExport.MEXTOTALQUANTITY] = this.txtSumSoLuong.Value;
                            //dr[InfoMedicine.MIMMEDICINEORIGINALNAME] = this.txtOriginalName.Text; // edit here  20090531
                            //dr[InfoMedicine.MEXMOREDETAILS] = this.txtMoreDetails.text; // edit here  200900602
                            //end phan them moi
                        }
                    }
                    this.txtNewMedicine.Text = "";
                    this.txtDonVi.Text = "";
                    this.cboCachSD.Text = "";//thaoht edit 20090726
                    this.tbSoLuong.Text = "";
                    this.txtDonVi.Text = "";
                    this.tbLanUong.Text = "";
                    this.tbNgayUong.Text = "";
                    this.cboSick.SelectedIndex = 0;
                    this.cboLuuY.Text = "";

                    //phan them moi
                    this.txtSumNgayDung.Value = 0;
                    this.txtSumSoLuong.Value = 0;
                    //phan them moi 20090602
                    this.txtOriginalName.Text = "";
                    this.txtMoreDetails.text = "";
                    //end phan them moi
                }
            }
        }

        private void cboCachSD_TextChanged(object sender, EventArgs e)
        {
            if (this.cboCachSD.Text != "-Chọn-")//thaoht edit 20090726
            {
                this.lbCuUong.Text = "Cử " + this.cboCachSD.Text + ":";
                this.lbLanUong.Text = "Lần " + this.cboCachSD.Text + ":";
                this.lbNgayUong.Text = "Ngày " + this.cboCachSD.Text + ":"; //thaoht edit 20090726
            }
        }

        private void cboSick_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cboSick.SelectedItem.ToString() != "-----------Chọn----------")
            {
                this.cboDonVi.Visible = false;
                this.txtDonViChuan.Visible = true;
                this.txtNewMedicine.Enabled = false;
                this.txtNewMedicine.Text = "";
                this.cboISN.SelectedIndex = this.cboSick.SelectedIndex - 1;

                ArrayList arrColumn = new ArrayList();
                arrColumn.Add(InfoMedicineImport.MIMPRODUCTIONPERUNIT);
                arrColumn.Add(InfoMedicineStock.MESQUANTITYINSTOCK);
                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoMedicineImport.MIMID, this.cboISN.SelectedValue.ToString()));
                DataTable dt = objController.GetData("vw_MedicineStock", arrColumn, param);

                string str = dt.Rows[0][InfoMedicineImport.MIMPRODUCTIONPERUNIT].ToString();
                this.txtSoLuongKho.Text = dt.Rows[0][InfoMedicineStock.MESQUANTITYINSTOCK].ToString();
                this.txtISN.Text = this.cboISN.SelectedValue.ToString();
                switch (str)
                {
                    case "0":
                        this.txtDonViChuan.Text = "viên";
                        this.txtDonVi.Text = "viên";
                        break;

                    case "1":
                        this.txtDonViChuan.Text = "ống";
                        this.txtDonVi.Text = "ống";
                        break;

                    case "2":
                        this.txtDonViChuan.Text = "chai";
                        this.txtDonVi.Text = "chai";
                        break;

                    case "3":
                        this.txtDonViChuan.Text = "gói";
                        this.txtDonVi.Text = "gói";
                        break;

                    case "4":
                        this.txtDonViChuan.Text = "tuýp";
                        this.txtDonVi.Text = "tuýp";
                        break;
                }

                //phan them moi
                this.txtOriginalName.Text = "";
                this.txtMoreDetails.text = "";//phan them moi 20090602
                this.tbSoLuong.Text = "";
                this.tbLanUong.Text = "";
                this.tbNgayUong.Text = "";
                this.cboLuuY.Text = "";
                this.cboDonVi.SelectedIndex = 0;
                this.cboCuUong.Text = "";
                cboCuUong.Items.Clear();

                ArrayList arrColumnMedImport = new ArrayList();
                arrColumnMedImport.Add(InfoMedicine.MIMMEDICINEORIGINALNAME);
                arrColumnMedImport.Add(InfoMedicine.MEXMOREDETAILS); //20090602
                arrColumnMedImport.Add(InfoMedicine.MEXQUANTITYPERUNIT);
                arrColumnMedImport.Add(InfoMedicine.MEXPRODUCTIONPERUNIT);
                arrColumnMedImport.Add(InfoMedicine.MEXTAKEPERDAY);
                arrColumnMedImport.Add(InfoMedicine.MEXQUANTITYPERDAY);
                arrColumnMedImport.Add(InfoMedicine.MEXUSAGEPERDAY);
                arrColumnMedImport.Add(InfoMedicine.MEXUSAGEUNIT);
                arrColumnMedImport.Add(InfoMedicine.MEXUSAGE);
                arrColumnMedImport.Add(InfoMedicine.MEXNOTE);
                param.Clear();
                param.Add(new ExpressionObject(InfoMedicine.MEDNAME, cboSick.SelectedItem.ToString()));

                DataTable dtMedImport = objController.GetData(InfoMedicine.TABLENAME, arrColumnMedImport, param);
                DataRow drMedImport = (dtMedImport != null && dtMedImport.Rows.Count > 0) ? dtMedImport.Rows[0] : null;
                if (drMedImport != null)
                {
                    this.txtOriginalName.Text = drMedImport[InfoMedicine.MIMMEDICINEORIGINALNAME].ToString();
                    this.txtMoreDetails.text = drMedImport[InfoMedicine.MEXMOREDETAILS].ToString(); //20090602
                    this.tbSoLuong.Text = drMedImport[InfoMedicine.MEXQUANTITYPERUNIT].ToString();

                    switch (drMedImport[InfoMedicine.MEXPRODUCTIONPERUNIT].ToString())
                    {
                        case "0":
                            cboDonVi.SelectedItem = "viên";
                            break;
                        case "1":
                            cboDonVi.SelectedItem = "ống";
                            break;

                        case "2":
                            cboDonVi.SelectedItem = "chai";
                            break;

                        case "3":
                            cboDonVi.SelectedItem = "gói";
                            break;

                        case "4":
                            cboDonVi.SelectedItem = "tuýp";
                            break;
                    }

                    this.tbNgayUong.Text = drMedImport[InfoMedicine.MEXTAKEPERDAY].ToString();
                    this.tbLanUong.Text = drMedImport[InfoMedicine.MEXQUANTITYPERDAY].ToString();
                    tbNgayUong_Leave(sender, e);
                    this.cboCuUong.Text = drMedImport[InfoMedicine.MEXUSAGEPERDAY].ToString();
                    this.txtDonVi.Text = drMedImport[InfoMedicine.MEXUSAGEUNIT].ToString();
                    this.cboCachSD.Text = drMedImport[InfoMedicine.MEXUSAGE].ToString().ToLower(); //thaoht edit 20090726
                    this.cboLuuY.Text = drMedImport[InfoMedicine.MEXNOTE].ToString();

                    //phan them moi
                    this.txtSumSoLuong.Value = Convert.ToDecimal(string.IsNullOrEmpty(this.tbSoLuong.Text) == true ? "0" : this.tbSoLuong.Text);
                    this.txtSumNgayDung.Value = 1;
                    //end phan them moi
                }
                else
                {
                    cboDonVi.SelectedItem = txtDonViChuan.Text;

                    //phan them moi
                    this.cboCachSD.SelectedIndex = 0; //edit here 20090531
                    this.txtSumSoLuong.Value = 0;
                    this.txtSumNgayDung.Value = 0;
                    //end phan them moi
                }
                //end phan them moi
            }
            else
            {
                this.txtNewMedicine.Enabled = true;
                this.cboDonVi.Visible = true;
                this.txtDonViChuan.Visible = false;
                this.txtISN.Text = "0";

                //phan them moi
                this.txtSumSoLuong.Value = 0;
                this.txtSumNgayDung.Value = 0;
                //end phan them moi
            }
        }

        private void cboToaMau_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cboToaMau.Text != "-----------Chọn----------")
            {
                ArrayList arrColumn = new ArrayList();
                arrColumn.Add(InfoMedicineExport.MEXORDER);
                arrColumn.Add(InfoMedicineImport.MIMID);
                arrColumn.Add(InfoMedicineExport.MEXMEDICINENAME);
                arrColumn.Add(InfoMedicineExport.MEXUSAGE);
                arrColumn.Add(InfoMedicineExport.MEXQUANTITYPERUNIT);
                arrColumn.Add(InfoMedicineExport.MEXPRODUCTIONPERUNIT);
                arrColumn.Add(InfoMedicineExport.MEXTAKEPERDAY);
                arrColumn.Add(InfoMedicineExport.MEXQUANTITYPERDAY);
                arrColumn.Add(InfoMedicineExport.MEXUSAGEPERDAY);
                arrColumn.Add(InfoMedicineExport.MEXUSAGEUNIT);
                arrColumn.Add(InfoMedicineExport.MEXNOTE);
                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoSamplePrescription.SAMSICKNAME, this.cboToaMau.Text));
                DataTable dtMedicineExport = objController.GetData(InfoSamplePrescription.TABLENAME, arrColumn, param);

                if (dtMedicineExport != null && dtMedicineExport.Rows.Count > 0)
                {
                    string text = string.Empty;
                    ListDictionary paramQuantityInStock = new ListDictionary();
                    for (int i = 0; i < dtMedicineExport.Rows.Count; i++)
                    {
                        paramQuantityInStock.Clear();
                        paramQuantityInStock.Add(InfoMedicineImport.MIMID, dtMedicineExport.Rows[i][InfoMedicineStock.MIMID].ToString());
                        object objQuantityInStock = objController.GetScalar(InfoMedicineStock.MESQUANTITYINSTOCK, paramQuantityInStock, "vw_MedicineStock");
                        if (objQuantityInStock != null && objQuantityInStock is Int64 && (Convert.ToInt64(dtMedicineExport.Rows[i][InfoMedicineExport.MEXQUANTITYPERUNIT].ToString()) > Convert.ToInt64(objQuantityInStock.ToString())))
                        {
                            string str4 = text;
                            text = str4 + "Số lượng thuốc '" + dtMedicineExport.Rows[i][InfoMedicineExport.MEXMEDICINENAME].ToString() + "' định Dùng lớn hơn số lượng thuốc còn tồn trong kho. \nSố lượng thuốc tồn trong kho : " + objQuantityInStock.ToString() + "\n";
                            dtMedicineExport.Rows.RemoveAt(i);
                        }
                    }
                    if (text != "")
                    {
                        MessageBox.Show(text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                   
                    dtView.Rows.Clear();// xóa toàn bộ toa thuốc đã được lập, để lấy thuốc theo toa thuốc mẫu --edit thaoht 20090603
                    DataRow drMedicineExport;
                    for (int j = 0; j < dtMedicineExport.Rows.Count; j++)
                    {
                        drMedicineExport = dtMedicineExport.Rows[j];
                        if (string.IsNullOrEmpty(drMedicineExport[InfoMedicineExport.MEXORDER].ToString())) //thaoht edit 20090726
                        {
                            drMedicineExport[InfoMedicineExport.MEXORDER] = j + 1;
                        }
                        MessageBox.Show(drMedicineExport[InfoMedicineExport.MEXORDER].ToString());
                        DataRow row = this.dtView.NewRow();
                        ArrayList list = new ArrayList();
                        list.Add(drMedicineExport[InfoMedicineExport.MEXORDER]);
                        list.Add(drMedicineExport[InfoMedicineExport.MEXMEDICINENAME]);
                        list.Add(drMedicineExport[InfoMedicineExport.MEXQUANTITYPERUNIT]);
                        switch (drMedicineExport[InfoMedicineExport.MEXPRODUCTIONPERUNIT].ToString())
                        {
                            case "0":
                                list.Add("viên");
                                break;
                            case "1":
                                list.Add("ống");
                                break;
                            case "2":
                                list.Add("chai");
                                break;
                            case "3":
                                list.Add("gói");
                                break;
                            case "4":
                                list.Add("tuýp");
                                break;
                        }

                        list.Add(drMedicineExport[InfoMedicineExport.MEXTAKEPERDAY]);
                        list.Add(drMedicineExport[InfoMedicineExport.MEXQUANTITYPERDAY]);
                        list.Add(drMedicineExport[InfoMedicineExport.MEXUSAGEUNIT]);
                        list.Add(drMedicineExport[InfoMedicineExport.MEXUSAGEPERDAY]);
                        list.Add(drMedicineExport[InfoMedicineExport.MEXUSAGE]);
                        list.Add(drMedicineExport[InfoMedicineExport.MEXNOTE]);
                        list.Add("1");
                        list.Add(drMedicineExport[InfoMedicineStock.MIMID]);
                        row[0] = list[0];
                        row[1] = list[1];
                        row[2] = list[2];
                        row[3] = list[3];
                        row[4] = list[4];
                        row[5] = list[5];
                        row[6] = list[6];
                        row[7] = list[7];
                        row[8] = list[8];
                        row[9] = list[9];
                        row[10] = list[10];
                        row[11] = list[11];
                        this.dtView.Rows.Add(row);
                    }
                    this.loadDataSet();
                }
            }
        }

        public bool check()
        {
            if ((this.txtNewMedicine.Text == "") && (this.txtDonViChuan.Text == ""))
            {
                MessageBox.Show("Vui lòng nhập đơn vị", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if ((this.txtNewMedicine.Text != "") && (this.cboDonVi.SelectedItem.ToString() == "-Chọn-"))
            {
                MessageBox.Show("Vui lòng chọn đơn vị", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if ((this.cboSick.Text == "") && (this.txtNewMedicine.Text == ""))
            {
                MessageBox.Show("Vui lòng chọn tên thuốc hoặc nhập tên thuốc mới", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if ((this.cboSick.SelectedItem.ToString() == "--------Chọn-------") && (this.txtNewMedicine.Text == ""))
            {
                MessageBox.Show("Vui lòng chọn tên thuốc hoặc nhập tên thuốc mới", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.cboCachSD.Text == "-Chọn-")//thaoht edit 20090726
            {
                MessageBox.Show("Vui lòng nhập Cách sử dụng", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.tbSoLuong.Text == "")
            {
                MessageBox.Show("Vui lòng nhập số lượng", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.tbLanUong.Text == "")
            {
                MessageBox.Show("Vui lòng nhập số lượng sử dụng cho một lần", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.txtDonVi.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đơn vị Dùng cho từng lần", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.tbNgayUong.Text == "-Chọn-")
            {
                MessageBox.Show("Vui lòng nhập số lần sử dụng trong ngày ", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.cboCuUong.Text == "")
            {
                MessageBox.Show("Vui lòng chọn cử uống", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (!this.IsNumber(this.tbSoLuong.Text))
            {
                MessageBox.Show("Vui lòng nhập kiểu số cho số lượng", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (!this.IsNumber(this.tbNgayUong.Text))
            {
                MessageBox.Show("Vui lòng nhập kiểu số cho số lần sử dụng trong một ngày", "Lỗi Nhập", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            return true;
        }

        private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
        {
            if ((this.dataGrid1.DataSource != null) && ((e.Button == MouseButtons.Left) && (this.dataGrid1.HitTest(e.X, e.Y).Type == DataGrid.HitTestType.RowHeader)))
            {
                DataRow dr = this.dsView.Tables["ExportMedicineList"].Rows[this.dataGrid1.CurrentRowIndex];

                if (dr["YesOrNo"].ToString() == "0")
                {
                    this.cboDonVi.Visible = true;
                    this.txtDonViChuan.Visible = false;
                    this.txtNewMedicine.Text = dr[InfoMedicineExport.MEXMEDICINENAME].ToString();
                    this.cboDonVi.SelectedItem = dr[InfoMedicineExport.MEXPRODUCTIONPERUNIT].ToString();
                    this.txtISN.Text = "0";
                }
                else
                {
                    this.cboDonVi.Visible = false;
                    this.txtDonViChuan.Visible = true;
                    this.cboSick.SelectedItem = dr[InfoMedicineExport.MEXMEDICINENAME].ToString();
                    this.txtDonViChuan.Text = dr[InfoMedicineExport.MEXPRODUCTIONPERUNIT].ToString();
                    this.txtISN.Text = dr[InfoMedicineExport.MEXISN].ToString();
                }

                this.tbSoLuong.Text = dr[InfoMedicineExport.MEXQUANTITYPERUNIT].ToString();
                this.tbNgayUong.Text = dr[InfoMedicineExport.MEXTAKEPERDAY].ToString();
                this.tbNgayUong_Leave(sender, e);
                this.tbLanUong.Text = dr[InfoMedicineExport.MEXQUANTITYPERDAY].ToString();
                this.txtDonVi.Text = dr[InfoMedicineExport.MEXUSAGEUNIT].ToString();
                this.cboCuUong.Text = dr[InfoMedicineExport.MEXUSAGEPERDAY].ToString();
                this.cboCachSD.Text = dr[InfoMedicineExport.MEXUSAGE].ToString().ToLower(); //thaoht edit 20090726
                this.cboLuuY.Text = dr[InfoMedicineExport.MEXNOTE].ToString();
                this.txtSTT.Text = dr[InfoMedicineExport.MEXORDER].ToString();

                //phan them moi 
                this.txtSumNgayDung.Value = Convert.ToDecimal((string.IsNullOrEmpty(dr[InfoMedicineExport.MEXTOTALUSAGEDAY].ToString()) == true || dr[InfoMedicineExport.MEXTOTALUSAGEDAY].ToString() == "0") ? "1" : dr[InfoMedicineExport.MEXTOTALUSAGEDAY].ToString());
                this.txtSumSoLuong.Value = Convert.ToDecimal((string.IsNullOrEmpty(dr[InfoMedicineExport.MEXTOTALQUANTITY].ToString()) == true || dr[InfoMedicineExport.MEXTOTALQUANTITY].ToString() == "0") ? this.tbSoLuong.Text : dr[InfoMedicineExport.MEXTOTALQUANTITY].ToString());
                //end phan them moi
            }
        }

        private void frmXemToaThuoc_Load(object sender, EventArgs e)
        {
            this.dtpDate.Value = DateTime.Now; //mac dinh load ngay hien tai len ngay in toa --20090602
            this.txtID.Text = this._fHoSoBenhAn.ID;
            this.txtTenBenhNhan.Text = this._fHoSoBenhAn.Ten;

            this.cboSick.Items.Add("-----------Chọn----------");
            this.cboToaMau.Items.Add("-----------Chọn----------");
            ArrayList arrColumn = new ArrayList();
            arrColumn.Add(InfoSamplePrescription.SAMSICKNAME);
            DataTable dtSamplePrescription = objController.GetData(InfoSamplePrescription.TABLENAME, true, arrColumn, null);
            if (dtSamplePrescription.Rows.Count > 0)
            {
                for (int i = 0; i < dtSamplePrescription.Rows.Count; i++)
                {
                    this.cboToaMau.Items.Add(dtSamplePrescription.Rows[i][0]);
                }
            }
            this.cboToaMau.SelectedIndex = 0;

            this.cboCachSD.Items.Add("-Chọn-");
            this.cboCachSD.Items.Add("uống");
            this.cboCachSD.Items.Add("xịt");
            this.cboCachSD.Items.Add("nhỏ tai");
            this.cboCachSD.Items.Add("bôi");
            this.cboCachSD.Items.Add("rửa mũi");
            this.cboCachSD.Items.Add("súc họng");
            this.cboCachSD.Items.Add("chích");
            this.cboCachSD.SelectedIndex = 0;

            this.cboDonVi.Items.Add("-Chọn-");
            this.cboDonVi.Items.Add("viên");
            this.cboDonVi.Items.Add("ống");
            this.cboDonVi.Items.Add("chai");
            this.cboDonVi.Items.Add("gói");
            this.cboDonVi.Items.Add("tuýp");
            this.cboDonVi.SelectedIndex = 0;

            this.cboLuuY.Items.Add("Uống trước khi ăn");
            this.cboLuuY.Items.Add("Uống sau khi ăn");
            this.cboLuuY.Items.Add("Uống trước khi ngủ 30 phút");
            this.cboLuuY.Items.Add("Xịt sau khi rửa mũi");
            this.cboLuuY.Items.Add("Thoa lên vết thương");
            this.cboLuuY.Items.Add("Thoa lên sẹo");

            //phan them moi
            ListDictionary orderby = new ListDictionary();
            orderby.Add(InfoMedicineStock.MESMEDICINENAME, "asc");
            //end phan them moi
            DataTable dtMedicineStock = objController.GetData("vw_MedicineStock", null, orderby);
            if (dtMedicineStock != null && dtMedicineStock.Rows.Count > 0)
            {
                for (int i = 0; i < dtMedicineStock.Rows.Count; i++)
                {
                    this.cboSick.Items.Add(dtMedicineStock.Rows[i][InfoMedicineStock.MESMEDICINENAME].ToString());
                }

                this.cboISN.DataSource = dtMedicineStock;
                this.cboISN.DisplayMember = InfoMedicineStock.MESMEDICINENAME;
                this.cboISN.ValueMember = InfoMedicineStock.MIMID;

                this.cboSick.SelectedIndex = 0;

                this.clrISN = this._fHoSoBenhAn.clrImageISN.ToString();
                this.lbMaBenhAn.Text = this.lbMaBenhAn.Text + " " + this.clrISN;

                ArrayList arrCoMedicineExport = new ArrayList();
                arrCoMedicineExport.Add(InfoMedicineExport.MEXORDER);//thaoht edit 20090726
                arrCoMedicineExport.Add(InfoMedicineExport.MEXMEDICINENAME + " as LanDung");
                arrCoMedicineExport.Add(InfoMedicineExport.MEXMEDICINENAME);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXQUANTITYPERUNIT);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXPRODUCTIONPERUNIT);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXTAKEPERDAY);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXQUANTITYPERDAY);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXUSAGEPERDAY);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXUSAGEUNIT);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXNOTE);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXSTATUS);
                arrCoMedicineExport.Add(InfoMedicineImport.MIMID);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXUSAGE);

                //phan moi them 
                arrCoMedicineExport.Add(InfoMedicineExport.MEXTOTALQUANTITY);
                arrCoMedicineExport.Add(InfoMedicineExport.MEXTOTALUSAGEDAY);
                arrCoMedicineExport.Add(InfoMedicine.MIMMEDICINEORIGINALNAME); //edit here 20090531
                arrCoMedicineExport.Add(InfoMedicine.MEXMOREDETAILS); //edit here 20090602
                //end phan moi them

                List<ExpressionObject> paramMedicineExport = new List<ExpressionObject>();
                paramMedicineExport.Add(new ExpressionObject(InfoClinicalRecord.CLRISN, this.clrISN));
                DataTable dtMedicineExport = objController.GetData("vw_AllPrescription", false, arrCoMedicineExport, paramMedicineExport);

                DataRow drMedicineExport;
                for (int j = 0; j < dtMedicineExport.Rows.Count; j++)
                {
                    drMedicineExport = dtMedicineExport.Rows[j];
                    if (string.IsNullOrEmpty(drMedicineExport[InfoMedicineExport.MEXORDER].ToString())) //thaoht edit 20090726
                    {
                        drMedicineExport[InfoMedicineExport.MEXORDER] = j + 1;
                    }
                   
                    switch (drMedicineExport[InfoMedicineExport.MEXPRODUCTIONPERUNIT].ToString())
                    {
                        case "0":
                            drMedicineExport["LanDung"] = "viên";
                            break;
                        case "1":
                            drMedicineExport["LanDung"] = "ống";
                            break;
                        case "2":
                            drMedicineExport["LanDung"] = "chai";
                            break;
                        case "3":
                            drMedicineExport["LanDung"] = "gói";
                            break;
                        case "4":
                            drMedicineExport["LanDung"] = "tuýp";
                            break;
                    }

                    DataRow row = this.dtView.NewRow();
                    row[0] = drMedicineExport[InfoMedicineExport.MEXORDER];
                    row[1] = drMedicineExport[InfoMedicineExport.MEXMEDICINENAME].ToString();
                    row[2] = drMedicineExport[InfoMedicineExport.MEXQUANTITYPERUNIT].ToString();
                    row[3] = drMedicineExport["LanDung"].ToString();
                    row[4] = drMedicineExport[InfoMedicineExport.MEXTAKEPERDAY].ToString();
                    row[5] = drMedicineExport[InfoMedicineExport.MEXQUANTITYPERDAY].ToString();
                    row[6] = drMedicineExport[InfoMedicineExport.MEXUSAGEUNIT].ToString();
                    row[7] = drMedicineExport[InfoMedicineExport.MEXUSAGEPERDAY].ToString();
                    row[8] = drMedicineExport[InfoMedicineExport.MEXUSAGE].ToString();
                    row[9] = drMedicineExport[InfoMedicineExport.MEXNOTE].ToString();
                    row[10] = drMedicineExport[InfoMedicineExport.MEXSTATUS].ToString();
                    row[11] = drMedicineExport[InfoMedicineStock.MIMID].ToString();

                    //phan moi them
                    row[12] = drMedicineExport[InfoMedicineExport.MEXTOTALQUANTITY].ToString();
                    row[13] = drMedicineExport[InfoMedicineExport.MEXTOTALUSAGEDAY].ToString();
                    row[14] = drMedicineExport[InfoMedicine.MIMMEDICINEORIGINALNAME].ToString(); //edit here 20090531
                    row[15] = drMedicineExport[InfoMedicine.MEXMOREDETAILS].ToString(); //edit here 20090602
                    //end phan moi them

                    this.dtView.Rows.Add(row);
                    this.arrTong.Add(row[11].ToString());
                }

                this.loadDataSet();
            }
        }

        private bool IsNumber(string Number)
        {
            try
            {
                long.Parse(Number);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void linkTenThuoc_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (this.cboSick.Visible)
            {
                this.cboSick.Visible = false;
                this.txtTenThuoc.Visible = true;
            }
            else
            {
                this.cboSick.Visible = true;
                this.txtTenThuoc.Visible = false;
            }
        }

        public void loadDataGrid()
        {
            DataGridTableStyle table = new DataGridTableStyle();
            table.MappingName = "ExportMedicineList";
            table.AlternatingBackColor = Color.AliceBlue;
            table.HeaderForeColor = Color.Maroon;
            table.GridLineColor = Color.CornflowerBlue;

            DataGridTextBoxColumn column = new DataGridTextBoxColumn();
            column.MappingName = InfoMedicineExport.MEXORDER;
            column.HeaderText = "STT";
            column.Width = 100;
            column.NullText = string.Empty;
            table.GridColumnStyles.Add(column);
           
            DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
            column2.MappingName = InfoMedicineExport.MEXMEDICINENAME;
            column2.HeaderText = "Tên Thuốc";
            column2.Width = 0xb9;
            column2.NullText = string.Empty;
            table.GridColumnStyles.Add(column2);

            DataGridTextBoxColumn column3 = new DataGridTextBoxColumn();
            column3.MappingName = InfoMedicineExport.MEXQUANTITYPERUNIT;
            column3.HeaderText = "Số Lượng";
            column3.Width = 120;
            column3.NullText = string.Empty;
            column3.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column3);

            DataGridTextBoxColumn column4 = new DataGridTextBoxColumn();
            column4.MappingName = InfoMedicineExport.MEXPRODUCTIONPERUNIT;
            column4.HeaderText = "Đơn Vị";
            column4.Width = 120;
            column4.NullText = string.Empty;
            column4.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column4);

            DataGridTextBoxColumn column5 = new DataGridTextBoxColumn();
            column5.MappingName = InfoMedicineExport.MEXTAKEPERDAY;
            column5.HeaderText = "Ngày SD/ Lần";
            column5.Width = 120;
            column5.NullText = string.Empty;
            column5.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column5);

            DataGridTextBoxColumn column6 = new DataGridTextBoxColumn();
            column6.MappingName = InfoMedicineExport.MEXQUANTITYPERDAY;
            column6.HeaderText = "Lần";
            column6.Width = 0x75;
            column6.NullText = string.Empty;
            column6.Alignment = HorizontalAlignment.Center;
            table.GridColumnStyles.Add(column6);

            DataGridTextBoxColumn column7 = new DataGridTextBoxColumn();
            column7.MappingName = InfoMedicineExport.MEXUSAGEUNIT;
            column7.HeaderText = "ĐV Sử Dụng";
            column7.Width = 120;
            column7.NullText = string.Empty;
            table.GridColumnStyles.Add(column7);

            DataGridTextBoxColumn column8 = new DataGridTextBoxColumn();
            column8.MappingName = InfoMedicineExport.MEXUSAGEPERDAY;
            column8.HeaderText = "Cử";
            column8.Width = 0xc3;
            column8.NullText = string.Empty;
            table.GridColumnStyles.Add(column8);

            DataGridTextBoxColumn column9 = new DataGridTextBoxColumn();
            column9.MappingName = InfoMedicineExport.MEXUSAGE;
            column9.HeaderText = "Cách Sử Dụng";
            column9.Width = 120;
            column9.NullText = string.Empty;
            table.GridColumnStyles.Add(column9);

            DataGridTextBoxColumn column10 = new DataGridTextBoxColumn();
            column10.MappingName = InfoMedicineExport.MEXNOTE;
            column10.HeaderText = "Lưu ý";
            column10.Width = 0xc3;
            column10.NullText = string.Empty;
            table.GridColumnStyles.Add(column10);

            DataGridTextBoxColumn column11 = new DataGridTextBoxColumn();
            column11.MappingName = "YesOrNo";
            column11.HeaderText = "";
            column11.Width = 0;
            column11.NullText = string.Empty;
            table.GridColumnStyles.Add(column11);

            DataGridTextBoxColumn column12 = new DataGridTextBoxColumn();
            column12.MappingName = InfoMedicineExport.MEXISN;
            column12.HeaderText = "";
            column12.Width = 0;
            column12.NullText = string.Empty;
            table.GridColumnStyles.Add(column12);

            this.dataGrid1.TableStyles.Add(table);
            table.AllowSorting = false;
            
        }

        public void loadDataSet()
        {
            this.dsView.Tables.Clear();
            this.dsView.Tables.Add(this.dtView);
            this.dataGrid1.TableStyles.Clear();
            this.dataGrid1.DataSource = this.dsView;
            this.dataGrid1.SetDataBinding(this.dsView, "ExportMedicineList");
            this.loadDataGrid();
        }

        private void tbLanUong_Leave(object sender, EventArgs e)
        {
            if (this.IsNumber(this.tbLanUong.Text))
            {
                if (Convert.ToInt64(this.tbLanUong.Text) == 0L)
                {
                    MessageBox.Show("Vui lòng nhập số liều Dùng cho một lần lớn hơn 0", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    this.tbLanUong.Text = "";
                }
            }
            else
            {
                ArrayList list = new ArrayList();
                foreach (string str in this.tbLanUong.Text.Trim().Split(new char[] { '/' }))
                {
                    list.Add(str);
                }
                if (list.Count >= 3)
                {
                    MessageBox.Show("Chỉ nên nhập 1 số có dạng A/B", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    this.tbLanUong.Text = "";
                    this.tbLanUong.Focus();
                }
                else
                {
                    try
                    {
                        long num2 = Convert.ToInt64(list[0].ToString());
                        long num3 = Convert.ToInt64(list[1].ToString());
                    }
                    catch
                    {
                        MessageBox.Show("Nhập kiểu số cho số liều sử dụng trong một lần", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        this.tbLanUong.Text = "";
                    }
                }
            }
        }

        private void tbNgayUong_Leave(object sender, EventArgs e)
        {
            if (this.tbNgayUong.Text != "")
            {
                this.textInComboCuUong(this.tbNgayUong.Text);
            }
        }

        public void textInComboCuUong(string ngay)
        {
            if (!this.IsNumber(ngay))
            {
                return;
            }
            ArrayList list = new ArrayList();
            list.Add("sáng");
            list.Add("trưa");
            list.Add("chiều");
            list.Add("tối");
            ArrayList list2 = new ArrayList();
            int num = Convert.ToInt32(ngay);
            if (num >= 5)
            {
                for (int k = 0; k < list.Count; k++)
                {
                    list2.Add(list[k].ToString());
                }
                for (int m = 0; m < list.Count; m++)
                {
                    if (m == (list.Count - 1))
                    {
                        break;
                    }
                    list2.Add(list[m].ToString() + "," + list[m + 1].ToString());
                    list2.Add(list[m].ToString() + "," + list[m + 2].ToString());
                    if (m == (list.Count - 3))
                    {
                        break;
                    }
                    list2.Add(list[m].ToString() + "," + list[m + 3].ToString());
                }
            }
            else
            {
                switch (num)
                {
                    case 1:
                        for (int n = 0; n < list.Count; n++)
                        {
                            list2.Add(list[n].ToString());
                        }
                        goto Label_0579;

                    case 2:
                        for (int num3 = 0; num3 < list.Count; num3++)
                        {
                            if (num3 == (list.Count - 1))
                            {
                                break;
                            }
                            list2.Add(list[num3].ToString() + "," + list[num3 + 1].ToString());
                            list2.Add(list[num3].ToString() + "," + list[num3 + 2].ToString());
                            if (num3 == (list.Count - 3))
                            {
                                break;
                            }
                            list2.Add(list[num3].ToString() + "," + list[num3 + 3].ToString());
                        }
                        break;

                    case 3:
                        for (int num4 = 0; num4 < list.Count; num4++)
                        {
                            if (num4 == (list.Count - 2))
                            {
                                break;
                            }
                            list2.Add(list[num4].ToString() + "," + list[num4 + 1].ToString() + "," + list[num4 + 2].ToString());
                            if (num4 == (list.Count - 3))
                            {
                                break;
                            }
                            list2.Add(list[num4].ToString() + "," + list[num4 + 1].ToString() + "," + list[num4 + 3].ToString());
                            list2.Add(list[num4].ToString() + "," + list[num4 + 2].ToString() + "," + list[num4 + 3].ToString());
                        }
                        goto Label_0579;

                    case 4:
                        list2.Add("sáng,trưa,chiều,tối");
                        goto Label_0579;

                    default:
                        goto Label_0579;
                }
                list2.Add(list[2].ToString() + "," + list[3].ToString());
                goto Label_0579;
            }
            list2.Add(list[2].ToString() + "," + list[3].ToString());
            for (int i = 0; i < list.Count; i++)
            {
                if (i == (list.Count - 2))
                {
                    break;
                }
                list2.Add(list[i].ToString() + "," + list[i + 1].ToString() + "," + list[i + 2].ToString());
                if (i == (list.Count - 3))
                {
                    break;
                }
                list2.Add(list[i].ToString() + "," + list[i + 1].ToString() + "," + list[i + 3].ToString());
                list2.Add(list[i].ToString() + "," + list[i + 2].ToString() + "," + list[i + 3].ToString());
            }
            list2.Add("sáng,trưa,chiều,tối");
        Label_0579:
            this.cboCuUong.Items.Clear();
            for (int j = 0; j < list2.Count; j++)
            {
                this.cboCuUong.Items.Add(list2[j].ToString());
            }
        }

        private void txtNewMedicine_TextChanged(object sender, EventArgs e)
        {
            if (this.txtNewMedicine.Text != "")
            {
                this.cboSick.SelectedIndex = 0;
                this.cboSick.Enabled = false;
                this.txtDonViChuan.Visible = false;
                this.cboDonVi.Visible = true;
            }
            else
            {
                this.txtDonViChuan.Visible = true;
                this.cboDonVi.Visible = false;
                this.cboSick.Enabled = true;
            }
        }

        private void txtTenThuoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                int num = this.cboSick.FindString(this.txtTenThuoc.Text);
                if (num >= 0)
                {
                    this.cboSick.Visible = true;
                    this.txtTenThuoc.Visible = false;
                    this.cboSick.SelectedIndex = num;
                }
                else
                {
                    MessageBox.Show("Tên thuốc này không tồn tại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }

        private void txtTenThuoc_Leave(object sender, EventArgs e)
        {
            int num = this.cboSick.FindString(this.txtTenThuoc.Text);
            if (num >= 0)
            {
                this.cboSick.Visible = true;
                this.txtTenThuoc.Visible = false;
                this.cboSick.SelectedIndex = num;
            }
            else
            {
                MessageBox.Show("Tên thuốc này không tồn tại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void txtSumNgayDung_Leave(object sender, EventArgs e)
        {
            this.txtSumSoLuong.Value = this.txtSumNgayDung.Value * Convert.ToDecimal(string.IsNullOrEmpty(this.tbSoLuong.Text) == true ? "0" : this.tbSoLuong.Text);
        }

        private void btnApplyAll_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.DataSource != null)
            {
                DataRow dr;
                for (int i = 0; i < this.dsView.Tables["ExportMedicineList"].Rows.Count; i++)
                {
                    dr = this.dsView.Tables["ExportMedicineList"].Rows[i];
                    dr[InfoMedicineExport.MEXTOTALUSAGEDAY] = this.txtSumNgayDung.Value;
                    dr[InfoMedicineExport.MEXTOTALQUANTITY] = this.txtSumNgayDung.Value * Convert.ToDecimal(string.IsNullOrEmpty(dr[InfoMedicineExport.MEXQUANTITYPERUNIT].ToString()) == true ? "0" : dr[InfoMedicineExport.MEXQUANTITYPERUNIT].ToString());
                }
            }
            
        }

        /// <summary>
        /// dieu chinh lai thu tu thuoc cua toa thuoc
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUp_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.DataSource != null)
            {
                DataRow drFocus = this.dsView.Tables["ExportMedicineList"].Rows[this.dataGrid1.CurrentRowIndex];
                string strSTT = drFocus[InfoMedicineExport.MEXORDER].ToString();
                if (strSTT.Equals("1"))
                {
                    return;
                }
                int intSTTPrevious = Convert.ToInt16(strSTT) - 1;
                foreach (DataRow dr in this.dsView.Tables["ExportMedicineList"].Select(InfoMedicineExport.MEXORDER + " =" + intSTTPrevious.ToString()))
                {
                    dr[InfoMedicineExport.MEXORDER] = strSTT;
                    break;
                }
                drFocus[InfoMedicineExport.MEXORDER] = intSTTPrevious.ToString();
            }
        }

        /// <summary>
        /// dieu chinh lai thu tu thuoc cua toa thuoc
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDown_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.DataSource != null)
            {
                DataRow drFocus = this.dsView.Tables["ExportMedicineList"].Rows[this.dataGrid1.CurrentRowIndex];
                string strSTT = drFocus[InfoMedicineExport.MEXORDER].ToString();
                if (strSTT.Equals(this.dsView.Tables["ExportMedicineList"].Rows.Count.ToString()))
                {
                    return;
                }
                int intSTTNext = Convert.ToInt16(strSTT) + 1;
                foreach (DataRow dr in this.dsView.Tables["ExportMedicineList"].Select(InfoMedicineExport.MEXORDER + " =" + intSTTNext.ToString()))
                {
                    dr[InfoMedicineExport.MEXORDER] = strSTT;
                    break;
                }
                drFocus[InfoMedicineExport.MEXORDER] = intSTTNext.ToString();
            }
        }

    }
}